<?php

class Users_Model extends CI_Model {
	
	/**
	 * User account register.
	 * @param array $data	/ All fields are necessary.
	 * The format is:
	 * array ( "USERID" => "you", 
	 * "PASSWORD" => "123", 
	 * "NICKNAME" => "WO", 
	 * "REALNAME" => "WO", 
	 * "GENDER" => "NAN", 
	 * "BIRTH" => "1987-07-07", 
	 * "ADDRESS" => "BJ", 
	 * "PORTRAITPATH" => "NO" );
	 */
	function register($data = array()) {
		$data['PASSWORD'] = md5($data['PASSWORD']);
		$user_das = new Users_Das ();
		$user_das->addUser ( $data );
	}
	
	function verify($userid, $password) {
		$md5_password = md5 ( $password );
		$user_das = new Users_Das ();
		$user = $user_das->getUser ( $userid );
		
		if ($user ['PASSWORD'] !== $md5_password)
			return false;
		return true;
	}
	
	function getProfile($userid) {
		$user_das = new Users_Das ();
		$user_das->getUser ( $userid );
	}
	
	function updateProfile($userid, $data = array()) {
		$user_das = new Users_Das ();
		$user_das->updateUser ( $userid, $data );
	}
	
	function addFriend($hostid, $friendid) {
		$relation_das = new Relations_Das ();
		$relation_das->addRelation ( array ('HOSTID' => $hostid, 'FRIENDID' => $friendid ) );
	}
	
	function getFriends($userid){
		$relation_das = new Relations_Das();
		$relations = $relation_das->getRelations($userid);
		
		$friendids = array();
		foreach ($relations as $relation)
			$friends[] = $relation['FRIENDID'];
			
		$user_das = new Users_Das();
		return $user_das->getUsers( $friendids);
	}
	
	function deleteFriend($hostid, $friendid) {
		$relation_das = new Relations_Das ();
		$relation_das->deleteRelation ( array ('HOSTID' => $hostid, 'FRIENDID' => $friendid ) );
	}
	
	function addCircle($data = array()){
		$circle_das = new Circles_Das();
		$circles = $circle_das->getCircles($data['USERID']);
		foreach ($circles as $circle) {
			if($circle['NAME'] === $data['NAME']){
				return false;
			}
		}
		
		$circle_das->addCircle($data);
		return true;
	}
	
	function getCircles($userid){
		$circle_das = new Circles_Das();
		return $circle_das->getCircles($userid);
	}
	
	function updateCircle($circleid, $data){
		$circle_das = new Circles_Das();
		$circle_das->updateCircle($circleid, $data);
	}
	
	function addFriendToCircle($circleid, $friendid){
		$circle_das = new Circles_Das();
		$circle = $circle_das->getCircle($circleid);
		
		$members = $circle['MEMBERS'];
		if($members !== "")
			$members .= ",|$friendid|";
		else
			$members .= "|$friendid|";
		$circle_das->updateCircle($circleid, array('MEMBERS'=>$members));
	}
	
	function deleteFriendFromCircle($circleid, $friendid){
		$circle_das = new Circles_Das();
		$circle = $circle_das->getCircle($circleid);
		
		$members = split(',', $circle['MEMBERS']);
		$new_members = "";
		foreach ($members as $member) {
			if($member !== "|$friendid|") {
				if($new_members === "")
					$new_members .= "$member";
				else
					$new_members .= ",$member";
			}
		}
		
		$circle_das->updateCircle($circleid, array('MEMBERS'=>$new_members));
	}
	
	function deleteCircle($circleid){
		$circle_das = new Circles_Das();
		$circle_das->deleteCircle($circleid);
	}
}